package wangm.service;


import wangm.bean.Pagination;
import wangm.bean.User;
import wangm.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户serviceImp
 *
 * @author zk
 * @date 2018/2/26
 */
@Service
public class UserServiceImp {
    /**
     * static logger
     */
    private static Logger logger = LoggerFactory.getLogger(UserServiceImp.class);

    @Autowired
    private UserRepository userRepository;
    /**
     * 检查是否登录
     *
     * @param number number
     * @param password password
     * @return is/not login
     */
    public User checkLogin(String number, String password){
        return userRepository.getUserByNumberAndPassword(number,password);
    }

    /**
     * 检查该用户名是否存在
     *
     * @param username username
     * @return is/not exist
     */
    public boolean isExistUsername(String username){
        User user = userRepository.getUserByUserName(username);
        return user == null ? false : true;
    }
    /**
     * 检查该学号是否存在
     *
     * @param number 学号
     * @return is/not exist
     */
    public boolean isExistNumber(String number){
        User user = userRepository.getUserByNumber(number);
        return user == null ? false : true;
    }

    /**
     * 根据id获取user
     *
     * @param id id
     * @return user
     */
    public User getUserById(String id){
        return userRepository.getUserById(id);
    }

    /**
     * 更新user
     *
     * @param user user
     */
    public void updateUser(User user){
        User originUser = getUserById(user.getId());
        if(!StringUtils.isEmpty(user.getUsername())){
            originUser.setUsername(user.getUsername());
        }
        if(!StringUtils.isEmpty(user.getPassword())){
            originUser.setPassword(user.getPassword());
        }
        userRepository.updateUser(originUser);
    }

    /**
     * 添加User
     *
     * @param user user
     */
    public void addUser(User user){
        if(StringUtils.isEmpty(user.getAdmin())){
            user.setAdmin("0");
        }
        userRepository.insertUser(user);
    }

    /**
     * 获取用户列表
     *
     * @param admin admin 1 是管理员 0 不是管理员
     */
    public List<User> getUserList(Integer admin, Pagination<User> pagination){
       return userRepository.getUserList(admin,pagination);
    }

    /**
     * 更新指定user
     *
     * @param user user
     */
    public void modifyUser(User user){
        userRepository.modifyUser(user);
    }

    /**
     * 移除指定id
     *
     * @param id id
     */
    public void removeUser(String id){
        userRepository.remvoeUser(id);
    }
}
